using Microsoft.IdentityModel.Tokens;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// 跨域配置
var corsname = "any";
builder.Services.AddCors(options =>
{
    options.AddPolicy(corsname, builder =>
    {
        builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
    });
});

// 添加Controllers服务
builder.Services.AddControllers();

// 添加Swagger服务
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// 加载Ocelot配置文件
builder.Configuration.AddJsonFile("OcelotConfig.json");

// 添加JWT认证服务
var authenticationProviderKey = "Create_JWT_By_JonJack_2024/5/23_For_SplLibrary_Project";
builder.Services.AddAuthentication()
    .AddJwtBearer(authenticationProviderKey, options =>
    {
        //options.Authority = "WebAppIssuer";
        //options.Audience = "WebAppAudience";
        // 其他JWT认证相关的配置
        options.RequireHttpsMetadata = false;
    });

// 添加Ocelot服务
builder.Services.AddOcelot(builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

// 使用跨域中间件
app.UseCors(corsname);

// 使用Ocelot中间件
app.UseOcelot();

// 使用认证中间件
app.UseAuthorization();

// 使用HTTPS重定向中间件
app.UseHttpsRedirection();

// 添加Controllers中间件
app.MapControllers();

app.Run();
